home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 223 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  3.1 KB

  1. Path: fido.asd.sgi.com!austern
  2. From: clamage@Eng.Sun.COM (Steve Clamage)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: new T[0] and sizeof(T)
  5. Date: 01 Feb 1996 17:38:53 PST
  6. Organization: Sun Microsystems Inc.
  7. Approved: austern@isolde.mti.sgi.com
  8. Message-ID: <4erovm$4hh@engnews1.Eng.Sun.COM>
  9. References: <1996Feb1.091641.4676@iiasa.ac.at>
  10. Reply-To: clamage@Eng.Sun.COM
  11. NNTP-Posting-Host: isolde.mti.sgi.com
  12. X-Original-Date: 2 Feb 1996 01:21:58 GMT
  13. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  14.     iQBVAwUBMRFrSky4NqrwXLNJAQGaUAH/UWFgkuOpuUIHGy2zhqatGNaazlVKADMt
  15.     xS6Va48FFyp/DExHx7Bz+dnLdeF3kl81bpBnmuPvpswydFuQf/IdMg==
  16.     =+3E+
  17. Originator: austern@isolde.mti.sgi.com
  18.  
  19. In article 4676@iiasa.ac.at, marek@iiasa.ac.at (Marek  MAKOWSKI) writes:
  20. >I would like to ask for comments on three easy questions illustrated 
  21. >by the following piece of code:
  22. >template <class I, class T>
  23. >void mVect<I,T>::resize(I new_size) {
  24. >   T *old = v;
  25. >   v = new T[new_size];    // T *v is a private member of mVect  
  26. >   int size_of_elem = sizeof(T);    // <--- question 3 
  27. >   // 
  28. >   // do something 
  29. >   // 
  30. >   delete[] old;     // <--- questions 1 & 2 
  31. >} 
  32. >I have the following questions:
  33. >1. Is it absolutely robust and portable to delete[] old, even
  34. >   if a previous call was for new_size == 0 (or if v was allocated
  35. >   by the ctor for the size == 0) ?
  36.  
  37. If you allocate an object with new[], you delete it with delete[]. In
  38. addition, it is always safe to delete a null pointer.
  39.  
  40. >    In other words: is it guaranteed that:
  41. >   (v = new T[0]) == 0;
  42.  
  43. No. The result of 'new' is never a null pointer (unless the allocation fails
  44. and you are using a pre-exception or "nothrow" version of new). A request of
  45. zero size results in a pointer value distinct from all other pointer values
  46. in the program. So if you write
  47.     T* t1 = new T[count];
  48.     T* t2 = new T[count];
  49. and if the allocations succeed, it must be true that
  50.     ( t1 != 0 && t1 != t2 && t2 != 0 )
  51. even if count is zero. The expression "new T[0]" is invalid, but if you
  52. use a non-const expression for the count, it is OK if the value is zero.
  53.  
  54. Since you have requested zero objects, you cannot dereference the resulting
  55. pointer -- it might not point at usable storage. But since you acquired the
  56. pointer value from "new" you can (and should) pass it to "delete".
  57.  
  58. >2. Is it correct to assume that no destructor is called by this statement
  59. >    if old was set as: old = new T[0]  ??
  60.  
  61. As noted above, "new T[0]" isn't valid, but if you ask for zero objects,
  62. no objects are created. When you delete the array, no destructors
  63. should be called. I can believe that a compiler might get some part of
  64. these requirements wrong, since this sort of thing hardly ever happens and
  65. bugs might slip through testing.
  66.  
  67. >3. Is there any risk involved in using sizeof(T) in this statement ?
  68.  
  69. If T is a completely-defined type, you can always ask for sizeof(T); its
  70. value is a compile-time constant. I'm afraid I don't understand this
  71. question.
  72.  
  73. ---
  74. Steve Clamage, stephen.clamage@eng.sun.com
  75. ---
  76. [ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  77.   Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy is
  78.   in http://reality.sgi.com/employees/austern_mti/std-c++/policy.html. ]
  79.